public class ShellSort {
    public static void main(String[] args) {
        int[] arr = {7, 5, 11, 2, 4, 6, 9, 1, 8, 3};
        System.out.println("排序前：");
        print(arr);

        shellSort(arr);
        System.out.println("排序后：");
        print(arr);
    }

    public static void shellSort(int[] arr) {
        int j = 0;
        int temp = 0;
        for (int increment = arr.length / 2; increment > 0; increment /= 2) {
            for (int i = increment; i < arr.length; i++) {
                temp = arr[i];
                for (j = i - increment; j >= 0; j -= increment) {
                    if (temp < arr[j]) {
                        arr[j + increment] = arr[j];
                    } else {
                        break;
                    }
                }
                arr[j + increment] = temp;
            }

        }
    }

    public static void print(int[] arr) {
        if (arr == null) return;
        for(int i : arr) {
            System.out.print(i + " ");
        }
        System.out.println();
    }
}